package com.customer.crm.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.crm.system.pojo.SystemFunction;
import com.customer.crm.system.pojo.SystemRole;
import com.customer.crm.system.pojo.SystemRoleFunction;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.service.SystemFunctionService;
import com.customer.crm.system.service.SystemRoleFunctionService;
import com.customer.crm.system.service.SystemRoleService;
import com.customer.crm.system.service.impl.SystemRoleServiceImpl;
import com.customer.crm.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * The type Role controller.
 *
 * @author Lyp
 * @create 2021 /12/17
 */
@Controller
@RequestMapping("/system")
public class RoleController {

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private SystemFunctionService systemFunctionService;

    @Autowired
    private SystemRoleFunctionService systemRoleFunctionService;

    /**
     * List role string.
     * 从数据库拿到路径  跳转页面
     *
     * @return the string
     */
    @RequestMapping("/role/RoleAction_list.action")
    public String listRole() {
        return "views/system/list_role";
    }

    /**
     * Page model map.
     * 分页拿到数据
     *
     * @param page the page
     * @param rows the rows
     * @return the model map
     */
    @RequestMapping("/role/RoleAction_findByPage.action")
    @ResponseBody
    public ModelMap page(Long page, Long rows) {

        List<SystemRole> listSystemRole = systemRoleService.page(page, rows);
        List<SystemRole> list = systemRoleService.list();

        ModelMap modelMap = new ModelMap();

        modelMap.put("totalRows", list.size());
        modelMap.put("result", listSystemRole);

        ModelMap modelMap1 = new ModelMap();
        modelMap1.put("data", modelMap);

        return modelMap1;
    }

    /**
     * To update string.
     * 携带Id 去修改页面
     *
     * @param userId the user id
     * @param model  the model
     * @return the string
     */
    @RequestMapping("/role/RoleAction_edit.action")
    public String toUpdate(@RequestParam("systemRoleId") String userId, Model model) {

        QueryWrapper<SystemRole> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        SystemRole systemRole = systemRoleService.getOne(wrapper);

        model.addAttribute("systemRole", systemRole);
        return "views/system/edit_role";
    }


    /**
     * Update result data.
     * 修改选中的用户信息
     *
     * @param systemRole the system role
     * @return the result data
     */
    @RequestMapping("/role/RoleAction_saveOrUpdate.action")
    @ResponseBody
    public ResultData update(SystemRole systemRole) {
        System.out.println(systemRole);
        boolean update = systemRoleService.updateById(systemRole);
        System.out.println(update);
        ResultData resultData = ResultData.buildSuccessResult("修改成功", update);
        return resultData;
    }


    /**
     * To add string.
     * 去添加页面
     *
     * @return the string
     */
    @RequestMapping("/role/RoleAction_toAdd.action")
    public String toAdd() {

        return "views/system/add_role";
    }


    /**
     * Add result data.
     * 添加角色信息
     *
     * @param systemRole the system role
     * @return the result data
     */
    @RequestMapping("/role/RoleAction_save.action")
    @ResponseBody
    public ResultData add(SystemRole systemRole) {
        boolean save = systemRoleService.save(systemRole);
        ResultData resultData = ResultData.buildSuccessResult("保存成功", save);

        return resultData;
    }

    /**
     * Remove result data.
     * 删除选中的
     *
     * @param ids the ids
     * @return the result data
     */
    @RequestMapping("/role/RoleAction_remove.action")
    public ResultData remove(String[] ids) {

        QueryWrapper<SystemRole> wrapper = new QueryWrapper<>();
        for (int i = 0; i < ids.length; i++) {
            wrapper.eq("id", ids[0]);
            systemRoleService.remove(wrapper);
        }

        ResultData resultData = ResultData.buildSuccessResult("删除成功", true);

        return resultData;
    }


    /**
     * Gets role action.
     * 加载权限页面
     *
     * @param roleId the role id
     * @param model  the model
     * @return the role action
     */
    @RequestMapping("/role/RoleAction_assign.action")
    public String getRoleAction(@RequestParam("systemRoleId") String roleId, Model model) {
        //将将选中的角色ID 放入请求域中
        model.addAttribute("roleId", roleId);
        return "views/system/assign_role";
    }


    /**
     * Find all result data.
     *
     * @return the result data
     */
    @RequestMapping("/function/FunctionAction_findAll.action")
    @ResponseBody
    public ResultData findAll() {
        ModelMap modelMap = new ModelMap();
        List<SystemFunction> list = systemFunctionService.list(new QueryWrapper<SystemFunction>());
        modelMap.put("data", list);
        ResultData resultData = ResultData.buildFailureResult("加载成功", list);
        return resultData;
    }

    /**
     * Find by role ids result data.
     * 没写好
     *
     * @param ids      the ids
     * @param modelMap the model map
     * @param model    the model
     * @return the result data
     */
    @RequestMapping("/function/FunctionAction_findByRoleIds.action")
    @ResponseBody
    public ResultData findByRoleIds(@RequestParam("ids") String ids, ModelMap modelMap, Model model) {
        QueryWrapper<SystemRole> wrapper = new QueryWrapper<>();
        //获取当前角色所有的权限信息
        List<SystemFunction> list = systemFunctionService.findByRoleIdCopy(ids);
        modelMap.put("length", list.size());
        model.addAttribute("data", modelMap);
        ResultData resultData = ResultData.buildSuccessResult("获取信息成功", list);
        return resultData;
    }

    /**
     * Update functions result data.
     * 给用户分配权限
     *
     * @param roleId the role id
     * @param ids    the ids
     * @return the result data
     */
    @RequestMapping("/role/RoleAction_updateFunctions.action")
    @ResponseBody
    public ResultData updateFunctions(String roleId, String[] ids) {

        Integer integer = systemRoleFunctionService.deleteByRoleId(roleId);
        SystemRoleFunction systemRoleFunction = new SystemRoleFunction();
        systemRoleFunction.setRoleId(roleId);
        boolean save = false;
        for (int i = 0; i < ids.length ; i++) {
            systemRoleFunction.setFuncId(ids[i]);
            save = systemRoleFunctionService.save(systemRoleFunction);
        }
        ResultData resultData = ResultData.buildSuccessResult("修改信息成功", save);
        return resultData;
    }
}
